package com.observer.custom.observer_object;

import com.observer.custom.inter.DisplayElement;
import com.observer.custom.inter.Observer;
import com.observer.custom.inter.Subject;

/**
 * @ClassName: StatisticsDisplay
 * @Author: ZhangYue26
 * @Description: 定义观察者对象：观察者对象，在初始化构造的时候，注册主题(成为观察主题的对象)
 * @Date: 2020-09-17
 */
public class StatisticsDisplay implements Observer, DisplayElement {

    private float temperature;//温度
    private float humidity;//湿度
    private float airpressure;//气压
    private Subject subject;//被观察者对象

    /**
     * @Description 在新建观察者模式时需要将该观察者添加至被观察者中的观察者List中
     * @author ZhangYue26
     * @date 2020/9/17
     * @param [subject]
     * @return
     **/
    public StatisticsDisplay(Subject subject) {
        this.subject = subject;
        this.subject.addObserver(this);
    }

    @Override
    public void display() {
        System.out.println("StatisticsDisplay == 气温:" + this.temperature + "\t" + "温度:" + this.humidity + "\t" + "气压:" + this.airpressure);
    }

    /**
      * @Description 被观察者通知当前观察者时调用
      * @author ZhangYue26
      * @date 2020/9/17
      * @param [temperature, humidity, airpressure]
      * @return void
      **/
    @Override
    public void update(float temperature, float humidity, float airpressure) {
        this.temperature = temperature;
        this.humidity = humidity;
        this.airpressure = airpressure;
        this.display();
    }
}
